import os
import shutil

import ddddocr
import requests
from PIL import Image

ocr = ddddocr.DdddOcr(show_ad=False)

with open('static/yzm/下载.png','rb') as f:
    r = ocr.classification(f.read())
    print(r)

# data:image/jpeg;base64,   需要去除头信息
# base = '/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAAyAMgDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD3+iiigDh/GPwo8NeNr03+opdxXxQR/aLecg4HQbWyv6VwT/BLxX4ccy+DvGksQzkQSs8IPsdpKt+KitrxR4r1zSfjx4e0VNQkXR7yGNmtgq7WZ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r6k+pNZS+APC6ahFfppEK3cUvnJKuQQ+Sc8dfvEYPGMegoA4j472bTeGln8sFVQqWDYPXhDg8gttcHGAYsfxjGJ8JL+8T4c6zPb27yyxw4hUIcbsEEKyktz8pwQMHcRwePTvH3hmXxT4Zl0+3KCVuAzKCQD1wT3/mM9DgjC+GHw5k8E294t1cGZpxsMZVdgGeSp64PHB9OlAHgXgzWbbwf44vW8S20s6GF4JFaM7gxxglWw2OOnB6VtfFedL+O21NtKmiuAscKzMvlRom0MAsYJPJLcljwT16j36T4deHJWU/YYkP8flwRIXX+6SEyF9hjrzmp7nwL4fudIuNMbToUt5gVHloA0YOOFPbpQBy/wAINRsNe8CG1imLBAYpo8hXB5y7YHyk54GSAqqB0Oec1X4ANrXia5v7jWVhs5naXZFD84Ls7Fc+xYAHHIrsfCfw+fwZrU02lXAWwuWYPa5by40zlSMksz9sk4Az65HfUAeV6j8G/COj+EtQ+x6aZ7lISxnuJC8hUctt6KG2ggEDrivGPg7oOkeIvGL2WqLI2It9v5bbWVwwO70PAxg/3vavriWJJ4nilQPG4wynoRXO2Hw/8MaXr0et6fpUdrfx7sSQsyg7lwcqDg/lQB4Z8cPFev2PioaDBf3FvYwW8ZURsV8wkfeJH5fhW/ffDzwR4b+GF5rWozrfalcWLiC8llZv3zIduxc4PzEdRniu8+JHwxsfH9rDJ532TUrcERXAXIZf7rDuP5Vzfgf4GQ6FqcOo+INRXU5Lb/j3t1U+Unud3X6cCgDyf4LeLbPwn43Z9QEnkX0H2QFBna7OhUn24P519c1ySfDHwVHetdjw7ZGdm37ipIB65AzgflXW0AFFFFABRRRQAUUUUAFFFFABRRRQAUUUUAFFFFABRRRQAUUUUAFFFFABRRRQAUUUUAFFFFABRRRQAUUUUAf/2Q=='
# r = ocr.classification(base)
# print(r)


# with open('static/yzm/yzm.jpg','rb') as f:
#     img = Image.open(f)
#     r = ocr.classification(img)
#     print(r)


# 识别在线图片
# res = requests.get('https://www.bjcourt.gov.cn/yzm.jpg', headers={
#     'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36'
# })
#
# # 保存图片
# directory_path = 'static/yzm'
# if os.path.exists(directory_path):
#     shutil.rmtree(directory_path)
# os.makedirs(directory_path)
# with open(f'{directory_path}/yzm.jpg', 'wb') as f:
#     f.write(res.content)
# r = ocr.classification(res.content)
# print(r)

